#include "kit/processor/UserProcessor.hpp"
#include <vector>
#include <string>


namespace KitProcessor
{
	UserProcessor::~UserProcessor()
	{
		// don't clean anything, we will do it when we are sure all processes finished
	}

	void UserProcessor::process(SystemSocket::Connection * iConnection)
	{
		this->_connection = iConnection;
		try
		{
			std::string aMessage;
			if(_connection->isValid())
			{
				aMessage = _connection->read();
				// forget last character
				aMessage = aMessage.substr(0, aMessage.size() - 1);

				if(aMessage != "" && _map->contains(aMessage) == false)
				{
					// on this connection message represens user id
					_map->insert(aMessage, new KitUser::User(iConnection));
				}
			}
			else
			{
				delete this->_connection;
			}
		}
		catch(SystemSocket::ReadException & aException)
		{
			// mark connections as invalid as we can't read aynomore from it
			// but keep processing so don't exit thread yet
			_connection->markInvalid();
			delete this->_connection;
		}

	}
}
